package cn.xspace.library.view.dialog;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.been.NavTagBean;

import java.util.ArrayList;
import java.util.List;

import cn.xspace.library.R;
import cn.xspace.library.util.DrawableUtils;

/**
 * Created by liuchen on 2016/12/5.
 */

public class PointDialog extends Dialog {
    private Context mContext;
    private GridView mGirdView;
    private ImageButton cancelBtn;
    private List<NavTagBean> pointList;
    private OperateAdapter mAdapter;
    private IPointClickListener pointClickListener;

    public PointDialog(Context context, int theme) {
        super(context, theme);
        View view = getLayoutInflater().inflate(R.layout.dialog_point,
                null);
        findView(view);
        mContext = context;
        super.setContentView(view);
    }


    public void setPointClickListener(IPointClickListener pointClickListener) {
        this.pointClickListener = pointClickListener;
    }

    public PointDialog(Context contex) {
        this(contex, R.style.CustomDialog);
    }

    public void setPointList(List<NavTagBean> pointList) {
        if (pointList.size() > 4) {
            mGirdView.setNumColumns(4);
        } else {
            mGirdView.setNumColumns(pointList.size());
        }

        this.pointList = pointList;
    }

    public void refreshView() {
        mAdapter.notifyDataSetChanged();
    }

    private void findView(View view) {
        mGirdView = (GridView) view.findViewById(R.id.gridview);
        cancelBtn = (ImageButton) view.findViewById(R.id.imgBtn_cancel);
        pointList = new ArrayList<>();
        mAdapter = new OperateAdapter();
        cancelBtn.setOnClickListener(mAdapter);
        mGirdView.setAdapter(mAdapter);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setGravity(Gravity.BOTTOM);
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        p.width = d.getWidth();
        getWindow().setAttributes(p);
    }


    public interface OnQuickOptionformClick {
        void onQuickOptionClick(int id);
    }

    //#7ac768 #6aa4d0 #dd916c #c6985d
    public static int[] backgroundColor = new int[]{Color.parseColor("#7ac768"), Color.parseColor("#6aa4d0"), Color.parseColor("#dd916c"), Color.parseColor("#c6985d")};

    public class OperateAdapter extends BaseAdapter implements
            android.view.View.OnClickListener {

        @Override
        public int getCount() {
            return pointList.size();
        }

        @Override
        public NavTagBean getItem(int position) {
            return pointList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return pointList.get(position).hashCode();
        }

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            NavTagBean ponit = getItem(position);
            View mView = LayoutInflater.from(mContext).inflate(R.layout.item_point, parent, false);
            mView.setOnClickListener(this);
            mView.setTag(ponit);
            ImageView logoView = (ImageView) mView.findViewById(R.id.img_operate);
            TextView nameView = (TextView) mView.findViewById(R.id.txt_name);
            RelativeLayout relative = (RelativeLayout) mView.findViewById(R.id.relative);
            if (position > -1 && position < backgroundColor.length) {
                relative.setBackground(DrawableUtils.getDrawable(mContext, backgroundColor[position], backgroundColor[position], 0, GradientDrawable.OVAL));
            }

            logoView.setImageBitmap(DrawableUtils.getBitmapbyAssentsName(ponit.navIconName));
            nameView.setText(ponit.navName);
            return mView;
        }

        @Override
        public void onClick(View v) {
            if (v.getTag() instanceof NavTagBean) {
                if (pointClickListener != null) {
                    pointClickListener.pointClick(((NavTagBean) v.getTag()));
                }
                dismiss();
            } else if (v.getId() == cancelBtn.getId()) {
                dismiss();
            }
        }
    }

    public interface IPointClickListener {
        void pointClick(NavTagBean poin_style);
    }

}
